<?php

namespace App\Models;


class ShopWaterBill extends BaseModel
{
    protected $table = "shop_waterbill";


    protected $rules = [
        'buyer_id' => 'required',
        'admin_id' => 'required',
        'shop_id' => 'required',
        'goods_id' => 'required',
        'goods_name' => 'required|between:1,20',
        'goods_price' => ['required', 'numeric', 'regex:/^([1-9]\d*(\.\d*[1-9])?)|(0\.\d*[1-9])/'],
        'goods_category_id' => 'required|numeric',
        'brand_id' => 'required|numeric',
        'bucket_category_id' => 'required|numeric',
        'waterbill_category' => 'required',
    ];

    protected $message = [
        'buyer_id.required' => '用户ID获取失败',
        'admin_id.required' => '商户ID获取失败',
        'shop_id.required' => '店铺ID获取失败',
        'goods_id.required' => '商品ID获取失败',
        'goods_name.required' => '商品名称必须填写',
        'goods_name.between' => '商品名称不能超过20个字',
        'goods_price.required' => '商品价格必须填写',
        'goods_price.numeric' => '商品价格必须是纯数字',
        'goods_price.regex' => '商品价格不能小于0',
        'goods_category_id.required' => '请选择商品所属分类',
        'goods_category_id.numeric' => '非法分类数据',
        'brand_id.required' => '请选择商品所属品牌',
        'brand_id.numeric' => '非法品牌数据',
        'bucket_category_id.required' => '请选择商品所属水桶分类',
        'bucket_category_id.numeric' => '非法水桶分类数据',
        'waterbill_category.required' => '请选择水票套餐',
    ];

    protected $casts = [
        'waterbill_category' => 'array'
    ];

    public function waterBill()
    {
        return $this->hasOne('App\Models\WaterBill', 'id', 'group_id');
    }


    public function getBillList()
    {
        $keyword = request('keyword');
        if ($keyword) {
            if (is_numeric($keyword)) {
                $where['buyer_id'] = $keyword;
            } else {
                $where['goods_name'] = ['like', '%' . $keyword . '%'];
            }
        }
        $rows = $this->_getPageList($where);
        return $rows;
    }

    public function getOwnGoodsList()
    {
        $keyword = request('keyword');
        $where['admin_id'] = is_login();
        if ($keyword) {
            if (is_numeric($keyword)) {
                $where['id'] = $keyword;
            } else {
                $where['goods_name'] = ['like', '%' . $keyword . '%'];
            }
        }
        $rows = $this->_getPageList($where);
        $txtStr['status'] = [1 => '上架', -1 => '下架', 0 => '待审核'];
        $rows['data'] = int_to_string($rows['data'], $txtStr);
        return $rows;
    }

    public function getGoodsListByShopId($shopId)
    {
        $keyword = request('keyword');
        $where['shop_id'] = $shopId;
        $where['status'] = ['>', 0];
        if ($keyword) {
            if (is_numeric($keyword)) {
                $where['id'] = $keyword;
            } else {
                $where['goods_name'] = ['like', '%' . $keyword . '%'];
            }
        }
        $rows = $this->_getPageList($where, 'id,goods_name,goods_num,goods_price');
        return $rows;
    }


    public function addFromStore()
    {
        $shopId = request('shop_id');
        $ids = request('ids');
        if (!(!empty($ids) && is_array($ids))) {
            $this->error = '请选择要操作的数据';
            return false;
        }
        $where['id'] = ['in', $ids];
        $where['admin_id'] = is_login();
        $goods = new Goods();
        $goodsList = $goods->_getList($where)->toArray();
        if (empty($goodsList)) {
            $this->error = '商品数据不存在';
            return false;
        }
        if (!$shopId) {
            $this->error = '非法操作';
            return false;
        }
        if (request('old')) {
            $old = explode(',', request('old'));
            $arr = array_diff($old, $ids);
            if (!empty($arr)) {
                foreach ($arr as $v) {
                    if (in_array($v, $old)) {
                        $_res = $this->checkData($arr);
                        if (!$_res) {
                            $this->error = '商品正在出售中无法删除';
                            return false;
                        }
                    }
                }
            }
            $ids = array_diff($ids, $old);
        }
        if (empty($ids)) {
            return true;
        }
        $data = [];
        foreach ($ids as $k => $v) {
            foreach ($goodsList as $n) {
                if ($n['id'] == $v) {
                    $data[] = $n;
                }
            }
        }
        if (empty($data)) {
            $this->error = '无效商品数据';
            return false;
        }
        foreach ($data as $k => $v) {
            $data[$k]['shop_id'] = $shopId;
            $data[$k]['goods_id'] = $v['id'];
            $data[$k]['addtime'] = time();
            $data[$k]['created_at'] = date('Y-m-d H:i:s');
            $data[$k]['updated_at'] = date('Y-m-d H:i:s');
            unset($data[$k]['id']);
        }
        foreach ($data as $v) {
            if (!$this->_validate($v)) {
                return false;
            }
        }
        $res = $this->_add($data);
        if (!$res) {
            $this->error = '添加失败，请稍后再试';
            return false;
        }
        $shop = new Shop();
        $shop->_update(['id'=>$shopId],['total_goods_num'=>count($ids)]);
        return $res;
    }

    public function checkData($ids)
    {
        if (!$ids) {
            $this->error = '参数错误';
            return false;
        }

        if (!is_array($ids)) {
            $ids = [$ids];
        }
        $where['goods_id'] = ['in', $ids];
        $where['shop_id'] = request('shop_id');
        $res = $this->_getList($where);
        if ($res) {
            return false;
        }
        return true;
    }

    public function addOrUpdate()
    {
        $ids = request('ids');
        if (!(!empty($ids) && is_array($ids))) {
            $this->error = '请选择要操作的数据';
            return false;
        }

        $waterBill = new WaterBill();
        $fields = 'admin_id,shop_id,goods_category_id,brand_id,bucket_category_id,waterbill_category,goods_id,goods_name,goods_price';
        $bill = $waterBill->_getOneById(request('id'),$fields);
        if (!$bill) {
            $this->error = '非法操作';
            return false;
        }
        $data = $bill->toArray();
        if (!$bill['waterbill_category']) {
            $this->error = '获取水票套餐信息错误';
            return false;
        }
        $category_id = request('category_id');
        foreach($bill['category_data'] as $v) {
            if ($category_id == $v['id']) {
                $data['waterbill_category'] = $v;
            }
        }
        $data['status'] = 0;
        $data['addtime'] = time();
        if (!$this->_validate($data)) {
            return false;
        }
        $res = $this->_save($data);
        if (!$res) {
            $this->error = '保存水票信息失败！';
            return false;
        }
        return $res;
    }

    public function del()
    {
        $id = request('id');
        if (!$id) {
            $this->error = '请选择要操作的数据!';
            return false;
        }
        $child = $this->_getList(['pid' => $id])->toArray();
        if (!empty($child)) {
            $this->error = '请先删除该品牌下的子品牌';
            return false;
        }
        $res = $this->_del(['id' => $id]);
        if (!$res) {
            $this->error = '删除品牌失败!';
            return false;
        }
        //记录行为
        action_log('update_shop', 'shop', $id);
        return '删除品牌成功';
    }


}
